package com.jzqf.androidserver;

import android.Manifest;
import android.os.Bundle;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.tbruyelle.rxpermissions3.RxPermissions;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import io.reactivex.rxjava3.functions.Consumer;


public class MainActivity extends AppCompatActivity {
    private ServerManager serverManager;
    private ExecutorService service = Executors.newCachedThreadPool();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        RxPermissions rxPermissions = new RxPermissions(this);
        rxPermissions.request(Manifest.permission.WRITE_EXTERNAL_STORAGE).subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                if (aBoolean) {
                    Toast.makeText(getBaseContext(), "同意", Toast.LENGTH_SHORT).show();
                    startServer();
                }
            }
        });
    }

    private void startServer() {
        service.submit(new Runnable() {
            @Override
            public void run() {
                serverManager = new ServerManager(MainActivity.this);
                serverManager.startServer();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        serverManager.stopServer();
    }
}